package com.yupi.springbootinit.datasource;

import com.yupi.springbootinit.model.enums.SearchEnum;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataSourceRegistry {

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private UserDataSource userDataSource;

    @Resource
    private PostDataSource postDataSource;

    private static Map<String, DataSource<?>> DATA_SOURCE_MAP;
    @PostConstruct
    private void doInit() {
        if (DATA_SOURCE_MAP == null) {
            DATA_SOURCE_MAP = new HashMap<>();
            DATA_SOURCE_MAP.put(SearchEnum.PICTURE.getValue(), pictureDataSource);
            DATA_SOURCE_MAP.put(SearchEnum.USER.getValue(), userDataSource);
            DATA_SOURCE_MAP.put(SearchEnum.POST.getValue(), postDataSource);
        }
    }

    public DataSource<?> getDataSourceByType(String type) {
        if (DATA_SOURCE_MAP == null) {
            return null;
        }
        return DATA_SOURCE_MAP.get(type);
    }
}
